# coding: UTF-8

def display():
    print("+---+---+---+")
    cell_line = "|"
    for i in range(9):
        cell_line += " " + cells[i] + " |"
        if i % 3 == 2:
            print(cell_line)
            cell_line = "|"
            print("+---+---+---+")

def mark(num):
    if num % 2:
        return "X"
    else:
        return "O"

LINES = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))

def is_lined(cells):
    for current in LINES:
        if (cells[current[0]] == "O" and cells[current[1]] == "O" and cells[current[2]] == "O") or \
           (cells[current[0]] == "X" and cells[current[1]] == "X" and cells[current[2]] == "X"):
            return True
    return False

if __name__ == '__main__':
    cells = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
    count = 0
    while count < 9 and not is_lined(cells):    # 手数が 9 より小さく、かつ同一マークが並んでいない間はループ
        row = input("Rows? : ")
        if row not in(1,2,3):
            print("Input 1, 2 or 3.")
            continue
        
        column = input("Column? : ")
        if column not in(1,2,3):
            print("Input 1, 2 or 3.")
            continue
        
        target_index = (row - 1) * 3 + (column - 1)
        if cells[target_index] == " ":
            cells[target_index] = mark(count)
        else:
            print("Not empty. Try another.")
            continue
        display()
        count += 1
    
    if is_lined(cells):                                     # 同一マークが並んでいたら
        print("Game over. " + mark(count - 1) + " won.")    # 勝者の表示
    else:                                                   # そうじゃなかったら
        print("Game over. Draw.")                           # 引き分け
